package com.shop.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.domain.ProductDetails;
import com.shop.goods.mapper.ProductDetailsMapper;
import com.shop.goods.service.ProductDetailsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 靳润波
* @description 针对表【goods_product_details(商品详情表)】的数据库操作Service实现
* @createDate 2024-08-01 11:20:28
*/
@Service
public class ProductDetailsServiceImpl extends ServiceImpl<ProductDetailsMapper, ProductDetails>
    implements ProductDetailsService{

    @Autowired
    private ProductDetailsMapper iProductDetailsMapper;

    @Override
    public List<ProductDetails> selectAll() {
        LambdaQueryWrapper<ProductDetails> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetails::getIsDelete,0);
        return iProductDetailsMapper.selectList(lqw);
    }


    @Override
    public List<ProductDetails> selectList(ProductDetails iProductDetails) {
        LambdaQueryWrapper <ProductDetails> lqw = this.lqw(iProductDetails);
        return iProductDetailsMapper.selectList(lqw);
    }

    @Override
    public Integer insert(ProductDetails iProductDetails) {
        return iProductDetailsMapper.insert(iProductDetails);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                ProductDetails iProductDetails = new ProductDetails();
                iProductDetails.setId(Long.parseLong(id));
                iProductDetails.setIsDelete(1);
                iProductDetailsMapper.updateById(iProductDetails);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(ProductDetails iProductDetails) {
        return iProductDetailsMapper.updateById(iProductDetails);
    }

    @Override
    public Integer reduceStock(Long productId, Integer num) {
        return iProductDetailsMapper.reduceStock(productId, num);

    }

    public LambdaQueryWrapper lqw(ProductDetails iProductDetails){
        LambdaQueryWrapper <ProductDetails> lqw = new LambdaQueryWrapper<>();
        if (iProductDetails.getId() != null){
            lqw.eq(ProductDetails::getId,iProductDetails.getId());
        }
        if (iProductDetails.getSpecId() != null){
            lqw.eq(ProductDetails::getSpecId,iProductDetails.getSpecId());
        }
        if (iProductDetails.getNum() != null){
            lqw.eq(ProductDetails::getNum,iProductDetails.getNum());
        }
        if (iProductDetails.getWeight() != null){
            lqw.eq(ProductDetails::getWeight,iProductDetails.getWeight());
        }
        if (iProductDetails.getPrice() != null){
            lqw.eq(ProductDetails::getPrice,iProductDetails.getPrice());
        }
        if (iProductDetails.getInventory() != null){
            lqw.eq(ProductDetails::getInventory,iProductDetails.getInventory());
        }
        if (iProductDetails.getImg() != null){
            lqw.eq(ProductDetails::getImg,iProductDetails.getImg());
        }
        if (iProductDetails.getProductId() != null){
            lqw.eq(ProductDetails::getProductId,iProductDetails.getProductId());
        }
        if (iProductDetails.getIsDelete() != null){
            lqw.eq(ProductDetails::getIsDelete,iProductDetails.getIsDelete());
        }
        if (iProductDetails.getCreateTime() != null){
            lqw.eq(ProductDetails::getCreateTime,iProductDetails.getCreateTime());
        }
        if (iProductDetails.getUpdateTime() != null){
            lqw.eq(ProductDetails::getUpdateTime,iProductDetails.getUpdateTime());
        }
        return lqw;
    }

}




